<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */
 
 namespace HiCommon\Repository;

use DB;
use HiCommon\Model\OrderCheckpointVehicle;

class OrderCheckpointVehicleRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\OrderCheckpointVehicle();
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return OrderCheckpointVehicle::commonget($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [])
    {
        return OrderCheckpointVehicle::commonpage($arr_option);
    }
    
    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return OrderCheckpointVehicle::where($arr_where)->commonpage($arr_option);

    }

    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return OrderCheckpointVehicle::where($arr_where)->count();
    }

    /**
     * 根据条件统计数量
     *
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_for_check_success($n_order_id,$arr_wherein)
    {
        return OrderCheckpointVehicle::where('order_checkpoint_id',$n_order_id)->wherein('check_state',$arr_wherein)->count();
    }

    /**
     * 统计数量- 主订单下已处理的车辆子单数量
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_for_deal_vehicle($n_id)
    {
        $arr_where = ['order_checkpoint_id'=>$n_id];
        return OrderCheckpointVehicle::where($arr_where)->whereIn('check_state',[2,9])->count();
    }

    /**
     * 统计数量- 主订单下申请处理流程的车辆子单数量
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_for_apply_vehicle($n_id)
    {
        $arr_where = ['order_checkpoint_id'=>$n_id];
        return OrderCheckpointVehicle::where($arr_where)->whereIn('apply_refund_state',[1,2,3,9])->count();
    }

    /**
     * 统计数量- 主订单下未处理的车辆子单数量
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_for_undeal_vehicle($n_id)
    {
        $arr_where = ['order_checkpoint_id'=>$n_id];
        return OrderCheckpointVehicle::where($arr_where)->whereIn('check_state',[0,1])->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return OrderCheckpointVehicle::where($arr_where)->sum($s_field);
    }
    
    /**
     * 根据条件批量删除信息
     *
     * @param array $arr_where
     * @param bool  $method
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_by_where($arr_where = [],$method = false)
    {
        return OrderCheckpointVehicle::where($arr_where)
                                    ->commondelete($method);
    }
    
    /**
     * 通过where获取一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where,$arr_option = [])
    {
        return OrderCheckpointVehicle::where($arr_where)->commonfirst($arr_option);
    }

    /**
     * 根据id查询一条数据
     *
     * @param       $n_id
     * @param array $arr_select
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function find_one($n_id,$arr_select = [])
    {
        return OrderCheckpointVehicle::select($arr_select)->find($n_id);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return OrderCheckpointVehicle::create($arr_input);
    }
    
    /**
     * 添加多条数据
     *
     * @param array $arr_data
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_many($arr_data)
    {
        return OrderCheckpointVehicle::insert($arr_data);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return OrderCheckpointVehicle::commonfind($n_id, $arr_option);
    }
    
    /**
     * 更新或新建一条数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_or_create($arr_where,$arr_input){
        $res = OrderCheckpointVehicle::where($arr_where)->first();
        if(is_null($res)){
            OrderCheckpointVehicle::create(array_merge($arr_where,$arr_input));
        }
        else{
            OrderCheckpointVehicle::where($arr_where)->update($arr_input);
        }
        return OrderCheckpointVehicle::where($arr_where)->first();
    }

    /**
     * 根据where更新数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_by_where($arr_where,$arr_input){
        return OrderCheckpointVehicle::where($arr_where)->update($arr_input);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return OrderCheckpointVehicle::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return OrderCheckpointVehicle::where('id', $n_id)
                             ->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return OrderCheckpointVehicle::where('id',$n_id)->update(['state'=>$n_state]);

    }
    
    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return OrderCheckpointVehicle::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return OrderCheckpointVehicle::where('id',$n_id)->decrement($s_field,$n_step);
    }



    /**
     * 根据条件统计订单数量(公众号)
     */
    public function count_by_where(array $data, array $array)
    {
        if($data['time_cate'] == 2){
            $raw = DB::raw("count(1) as total, DATE_FORMAT(created_at,'%Y-%m') as date, sum(price) as price");
            $between = [reset($array), date('Y-m-d H:i:s', time())];
        }else{
            $raw = DB::raw("count(1) as total, DATE_FORMAT(created_at,'%Y-%m-%d') as date, sum(price) as price");
            $between = [reset($array), end($array) . ' 23:59:59'];
        }

        //订单统计(代驾)
        $days[1] = OrderCheckpointVehicle::select($raw)
            ->whereBetween('created_at', $between)
            ->where(['is_call_helpdriving' => 1])
            ->whereIn('order_checkpoint_id',$data['order_ids'])
            ->groupBy('date')
            ->get()->toArray();

        //订单统计(自驾)
        $days[2] = OrderCheckpointVehicle::select($raw)
            ->whereBetween('created_at', $between)
            ->where(['is_call_helpdriving' => 2])
            ->whereIn('order_checkpoint_id',$data['order_ids'])
            ->groupBy('date')
            ->get()->toArray();

        //总共的订单数
        $days[3] = OrderCheckpointVehicle::whereBetween('created_at', $between)
            ->whereIn('order_checkpoint_id',$data['order_ids'])
            ->count();

        return $days;
    }

    /*企业车辆检测记录*/
    public function company_vehicle_check_records($n_company_id = 0,$arr_checked_at = []){
        $s_date_format = '%Y-%m-%d';
        $arr_select    = ['id','users_vehicle_id','check_state','checked_at'];
//        $arr_select    = ['id','users_vehicle_id','check_state','checked_at',DB::raw('date_format(checked_at,"' . $s_date_format . '") as checked_date')];

        $result = self::base_model()
                      ->with(['user_vehicle'])
                      ->whereHas('user_vehicle',
                        function ($query)
                        use ($n_company_id) {
                            $query->where('users_company_id', $n_company_id);
                        })
                      ->whereIn('check_state', [2, 3])
                      ->whereBetween('checked_at', $arr_checked_at)
                      ->orderByDesc('checked_at')
                      ->select($arr_select)
                      ->get();

        return $result;
    }

    /*年检预约提醒车辆列表*/
    public function waiting_for_remind_vehicle_list(){
        $arr_option  = [
          'field'=>['id','order_sn']
        ];

        //提前15天提醒
        $date_time = date('Y-m-d',strtotime('-1 year +15 day'));
        $arr_between_time = [$date_time . ' 00:00:01', $date_time . ' 23:59:59'];

        $result = self::base_get_option($arr_option)
                      ->whereBetween('checked_at', $arr_between_time)
                      ->whereIn('check_state', ['2', '3'])
                      ->groupBy('plate_no')
                      ->get();

        return $result;

    }

}